package com.carkeeper.tweet.controller;

import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.tweet.pojo.Tweet;
import com.carkeeper.tweet.service.TweetService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: carkeeper_parent
 * @Date: 2020/11/23 14:19
 * @Author: qgg
 * @Description:
 */
@RestController
@RequestMapping("/tweet")
@Api(tags = "分类管理接口api")
public class TweetController {


    @Autowired
    private TweetService tweetService;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    /**
     * 创建索引结构
     * @return
     */
    @GetMapping("/create")
    @ApiOperation("创建索引结构接口")
    public Result create(){
        tweetService.createIndexAndMapping();
        return new Result(true , StatusCode.OK , "创建索引库结构成功");
    }

    /**
     * 一键导入索引库
     * @return
     */
    @GetMapping("/importAll")
    @ApiOperation("一键导入索引库接口")
    public Result importAll(){
        tweetService.importAll();
        return new Result(true , StatusCode.OK , "导入全部数据成功");
    }

    //查询所有
    @GetMapping("/findAll")
    @ApiOperation("查询所有推文接口")
    public Result findAll(){
        List<Tweet> tweetList = tweetService.findAll();
        return new Result(true , StatusCode.OK , "查询成功" , tweetList);
    }

    //添加
    @PostMapping("/add")
    @ApiOperation("添加推文接口")
    public Result add(@RequestBody Tweet tweet){
        tweetService.add(tweet);
        return new Result(true , StatusCode.OK , "添加成功");
    }

    //删除
    @DeleteMapping("/delete/{id}")
    @ApiOperation("通过id删除推文接口")
    public Result delete(@PathVariable("id") String id){
        tweetService.delete(id);
        return new Result(true , StatusCode.OK , "删除成功");
    }

    //修改
    @PutMapping("/update")
    @ApiOperation("修改推文接口")
    public Result update(@RequestBody Tweet tweet){
        tweetService.update(tweet);
        return new Result(true , StatusCode.OK , "修改成功");
    }

    //根据id查询数据
    @GetMapping("/findById/{id}")
    @ApiOperation("通过id查询推文接口")
    public Result findById(@PathVariable("id") String id){
        Tweet tweet = tweetService.findById(id);
        return new Result(true , StatusCode.OK , "查询成功" , tweet);
    }

    //多条件分页查询
    @GetMapping("/findList/{currentPage}/{pageSize}")
    @ApiOperation("多条件分页查询接口")
    @ApiImplicitParam(name = "searchMap",required = false)
    public Result findList(@RequestParam Map<String , Object> searchMap , @PathVariable("currentPage") String currentPage, @PathVariable("pageSize") String pageSize){
        List<Tweet> tweetList = tweetService.findList(searchMap, currentPage, pageSize);
        return new Result(true , StatusCode.OK , "查询成功" , tweetList);
    }

    //内容查询
    @GetMapping("/searchCategory")
    @ApiOperation("内容查询接口")
    @ApiImplicitParam(name = "searchMap",required = false)
    public Result searchCategory(Map<String , String> map){
        Map<String, Object> category = tweetService.searchCategory(map);
        return new Result(true , StatusCode.OK , "查询成功" , category);
    }

}
